package com.seio.edc.controller.recentpay;

import java.text.ParseException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.nova.edc.model.system.CustomerInfo;
import com.nova.edc.model.system.Login;
import com.nova.edc.model.system.Payment;
import com.nova.edc.service.system.CustomerInfoService;
import com.nova.edc.service.system.CustomerMapService;
import com.nova.edc.service.system.PaymentService;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.nova.frame.utils.StringUtils;
import com.seio.edc.util.DateUtil;

/**
 * 近期缴费
 */
@Controller
@RequestMapping(value = "/recentPay")
public class RecentPayController {

	@Autowired
	CustomerMapService customerMapService;

	@Autowired
	private CustomerInfoService novaCustomerInfoService;

	@Autowired
	private PaymentService novaPaymentService;

	private final int PAGE_SIZE = 10;

	/**
	 * 近期缴费列表
	 * 
	 * @throws ParseException
	 */
	@RequestMapping(value = "list")
	public ModelAndView list(HttpServletRequest request, HttpSession session,
			ModelMap modelMap, Integer pageIndex, CustomerInfo customerInfo,
			String searchDate, String customerName) throws ParseException {
		Login login = (Login) session.getAttribute("sessionLogin");
		customerInfo.setAddby(login.getUsername());
		// 获取需要查询的缴费日期
		Date currentDate = StringUtils.isNullOrEmpty(searchDate) ? new Date()
				: DateUtil.stringToDate(searchDate,
						DateUtil.DEFAULT_DATE_FORMAT_YM);
		modelMap.put("searchDate", DateUtil.dateToString(currentDate,
				DateUtil.DEFAULT_DATE_zh_CN_FORMAT_YM));
		// 获取需要查询的日期的年份和月份
		int year = DateUtil.getYear(currentDate);
		int month = DateUtil.getMonth(currentDate);
		modelMap.put(DateUtil.DEFAULT_YEAR, year);
		modelMap.put(DateUtil.DEFAULT_MONTH, month);
		PageList<CustomerInfo> remindList = null;
		Page page = new Page();
		page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
		page.setPageSize(PAGE_SIZE);
		remindList = novaCustomerInfoService.findPaymentPageList(
				login.getUsername(), year, month, customerName, page);
		modelMap.put("pageIndex", page.getCurrentPage());
		modelMap.put("dataCount", page.getRowCount());
		modelMap.put("pageCount", page.getTotalPage());
		modelMap.put("remindList", remindList.getDatas());
		return new ModelAndView("recentpay/recentPayList", modelMap);
	}

	/**
	 * 通过ajax修改客户信息
	 */
	@RequestMapping(value = "addPay.shtml")
	@ResponseBody
	public Payment addPay(HttpServletRequest request, HttpSession session,
			ModelMap modelMap, @RequestBody Payment payment) {
		return novaPaymentService.insert(payment);
	}
}
